import axios from "axios"
import { Message } from "@arco-design/web-vue"
// import { store } from "./index"

interface HTTP_RESPONSE {
    success: 1 | 0
    data: any
    msg: string
}

const instance = axios.create({
    baseURL: '/api',
    timeout: 60000,
})

instance.interceptors.request.use(
    function (config: any) {
        config.headers = {
            'x-requested-with': 'demo',
        }
        return config
    },
    function (error) {
        return Promise.reject(error)
    }
)

instance.interceptors.response.use(
    function (response) {
        if (response.data.msg) {
            Message.success(response.data.msg)
        }
        return response.data.data as HTTP_RESPONSE["data"]
    },
    function (error) {
        if (error && error.response) {
            switch (error.response.status) {
                case 401:
                    // 客户端环境
                    if (window) {
                        // store.remove("userInfo")
                        location.href = "/user/login"
                    }
                    break
                case 500:
                case 503:
                    Message.error(error.response.data.msg)
                    break
                case 501:
                    // 没有权限
                    window && (location.href = "/user/forbin")
                    break
            }
        }
        return Promise.reject(error)
    }
)

export default instance

